import Utils.GetFileEncodingByPath;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import org.bson.Document;

import java.io.*;

public class ReadEncodingFile {
    public static void main(String[] args) {
        MongoClient mongoClient = new MongoClient("192.168.1.171", 30000);
        MongoCollection<Document> target = mongoClient.getDatabase("aitexts_质押冻结").getCollection("RawData");
        File[] f_all = new File("D:\\公告大全\\质押冻结_TXT").listFiles();
        int count = 0;
        for (File f: f_all) {
            Document document = new Document("_id",f.getName());
            String name = f.getName();
//            System.out.println(name);
            String[] nameAll = name.split("_");
//            System.out.println(name);
            if (nameAll.length >= 4){
                String Year = nameAll[3].substring(0,4);
                if(nameAll[0].contains(".")){
                    String Scode = nameAll[0].substring(0,nameAll[0].indexOf("."));
                    String Sname = nameAll[1];
//                    String Beigao = nameAll[2];
//                System.out.println(Scode);
//                System.out.println(Year);
                    String Date = nameAll[3];
                    document.append("Year",Year);
                    document.append("Scode",Scode);
                    document.append("Sname",Sname);
//                    document.append("Beigao",Beigao);
                    document.append("Date",Date);
                }

//                System.out.println(Date);
                System.out.println(document);
            }
//            System.out.println(document);

//            System.out.println(Year);
//            document.append("Date",Date);

//            System.out.println(Date);
//            if(nameAll[1].contains("证券")){
//                String Qname = nameAll[1].substring(0,nameAll[1].indexOf("证券")+2);
//                document.append("Qname",Qname);
//            }
//            document.append("B_name",nameAll[0]);
            String content = txt2String(f);
//            System.out.println(document);
            if(content.trim().length() > 0){
//                document.append("Year",Year);
//                System.out.println(document.getString("_id"));
                document.append("content",content);

                target.updateOne(new BasicDBObject("_id", document.getString("_id")), new BasicDBObject("$set", document), (new UpdateOptions()).upsert(true));
            }

            System.out.println(count++);
        }
    }
    public static String txt2String(File file){
        StringBuilder content = new StringBuilder();
        try {
            String coding = GetFileEncodingByPath.getEncodingByPath(file.getAbsolutePath());
            InputStreamReader streamReader = new InputStreamReader(new FileInputStream(file),coding);
            BufferedReader bufferedReader = new BufferedReader(streamReader);
            String line;
            while ((line = bufferedReader.readLine())!=null){
                content.append(line);
            }
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content.toString();
    }
}
